﻿var VendorValidationClientSideHide = false;
var CurrentTab = 1;
var CloseContractDetail = false;
var CurrentTabElement = null;
var PageNumber = 1;
var IsSearchView = false;
var VendorIDGlobal = null;
var SelectedCountryID = null;
var Phone800 = null;
var PhoneSales = null;
var FaxSales = null;
var PhoneNumber = null;
var FaxNumber = null;
var AddCurrencyDropVal = 1;
var AddServiceDropVal = 1;
var AddSeasonCodeDropVal = 1;
$(document).ready(function () {
    var path = window.location.pathname;
    if (path.indexOf("ManagePreferredVendors") != -1) {
        $.validator.addMethod("ManagePreferredVendorRequireds", $.validator.methods.required, Messages["ManagePreferredVendorRequireds"]);
        $.validator.addMethod("ManagePreferredVendorDate", $.validator.methods.datePicker, Messages["ManagePreferredVendorDate"]);
        $.validator.addMethod("ManagePreferredVendorEmail", $.validator.methods.email, Messages["ManagePreferredVendorEmail"]);
        $.validator.addMethod("ManagePreferredVendorMaxLength20", $.validator.methods.maxlength, Messages["ManagePreferredVendorMaxLength20"]);
        $.validator.addMethod("ManagePreferredVendorMaxLength30", $.validator.methods.maxlength, Messages["ManagePreferredVendorMaxLength30"]);
        $.validator.addMethod("ManagePreferredVendorMaxLength50", $.validator.methods.maxlength, Messages["ManagePreferredVendorMaxLength50"]);
        $.validator.addMethod("ManagePreferredVendorMaxLength100", $.validator.methods.maxlength, Messages["ManagePreferredVendorMaxLength100"]);
        $.validator.addMethod("ManagePreferredVendorMaxLength250", $.validator.methods.maxlength, Messages["ManagePreferredVendorMaxLength250"]);
        $.validator.addClassRules({
            Required: {
                ManagePreferredVendorRequireds: true,
            },
            Email: {
                ManagePreferredVendorEmail: true,
            },
            Date: {
                ManagePreferredVendorDate: true,
            },
            MaxLength250: {
                ManagePreferredVendorMaxLength250: 250,
            },
            MaxLength100: {
                ManagePreferredVendorMaxLength100: 100,
            },
            MaxLength50: {
                ManagePreferredVendorMaxLength50: 50,
            },
            MaxLength30: {
                ManagePreferredVendorMaxLength30: 30,
            },
            MaxLength20: {
                ManagePreferredVendorMaxLength20: 20,
            },
        });

        $('#LoadPreferredVendorDetailsForm').validate();
        $('#LoadPreferredVendorContractsForm').validate();
        $('#LoadAddPreferredVendorContractsForm').validate();
        $('#LoadPreferredVendorContractDetailsForm').validate();
        $('#LoadAddPreferredVendorContractDetailsForm').validate();
        LoadVendorGrid();
        $(".Tab").click(function (event) {

            if ($(this).hasClass("active")) {
                return false;
            }
            if ($(this).hasClass("Tab")) {
                CurrentTabElement = this;
            }
            if ((CurrentTab == 1 && !($('#VendorDetailsSaveBtn').hasClass('BackColorRed'))) || (CurrentTab == 2 && !($('#VendorContractsSaveBtn').hasClass('BackColorRed')) && !($('#AddVendorContractBtn').hasClass('BackColorRed'))) || (CurrentTab == 3 && !($('#VendorContractDetailsSaveBtn').hasClass('BackColorRed')) && !($('#AddVendorContractDetailBtn').hasClass('BackColorRed'))) || CurrentTab == 4) {
                switch (CurrentTab) {
                    case 1:
                        {
                            $("#VendorGeneral").removeClass("active");
                            $("#tabGeneral").removeClass("active in");
                            VendorValidationClientSideHide = false;
                        }
                        break;
                    case 2:
                        {
                            $("#VendorContracts").removeClass("active");
                            $("#tabContracts").removeClass("active in");
                            VendorValidationClientSideHide = false;
                        }
                        break;
                    case 3:
                        {
                            $("#VendorContractDetails").removeClass("active");
                            $("#tabContractDetails").removeClass("active in");
                            VendorValidationClientSideHide = false;
                        }
                        break;
                    case 4:
                        {
                            $("#VendorGroups").removeClass("active");
                            $("#tabGroups").removeClass("active in");
                            VendorValidationClientSideHide = false;
                        }
                        break;
                    default:
                        break;
                }
                switch ($(CurrentTabElement).attr("id")) {
                    case "VendorGeneral":
                        {
                            CurrentTab = 1;
                            $("#VendorGeneral").addClass("active");
                            $("#tabGeneral").addClass("active in");
                            VendorValidationClientSideHide = false;
                        }
                        break;
                    case "VendorContracts":
                        {
                            if (CloseContractDetail) {
                                $("#AddVendorContractDetailBtn").removeClass('BackColorRed');
                                $("#VendorContractDetailsSaveBtn").removeClass('BackColorRed');
                                $("#VendorContractDetails").removeClass("active");
                                $("#tabContractDetails").removeClass("active in");
                                $("#VendorContractDetails").hide();
                                CloseContractDetail = false;
                            }
                            CurrentTab = 2;
                            $("#VendorContracts").addClass("active");
                            $("#tabContracts").addClass("active in");
                            VendorValidationClientSideHide = false;
                        }
                        break;
                    case "VendorContractDetails":
                        {
                            CurrentTab = 3;
                            $("#VendorContractDetails").addClass("active");
                            $("#tabContractDetails").addClass("active in");
                            $("#VendorContractDetails").show();
                            VendorValidationClientSideHide = false;
                        }
                        break;
                    case "VendorGroups":
                        {
                            CurrentTab = 4;
                            $("#VendorGroups").addClass("active");
                            $("#tabGroups").addClass("active in");
                            VendorValidationClientSideHide = false;
                        }
                        break;
                    default:
                        break;
                }
                return false;
            }
            event.preventDefault();
            event.stopImmediatePropagation();
            if ($('#VendorDetailsSaveBtn').hasClass('BackColorRed') && CurrentTab == 1) {
                jConfirm(Messages["ChangeTab"], Messages["ChangeTabTitle"], function (retVal) {
                    if (retVal != true) {
                        event.stopImmediatePropagation();
                        event.preventDefault();
                    }
                    else {
                        $("#VendorGeneral").removeClass("active");
                        $("#tabGeneral").removeClass("active in");
                        switch ($(CurrentTabElement).attr("id")) {
                            case "VendorContracts":
                                {
                                    CurrentTab = 2;
                                    $("#VendorContracts").addClass("active");
                                    $("#tabContracts").addClass("active in");
                                    VendorValidationClientSideHide = false;
                                }
                                break;
                            case "VendorContractDetails":
                                {
                                    CurrentTab = 3;
                                    $("#VendorContractDetails").addClass("active");
                                    $("#tabContractDetails").addClass("active in");
                                    $("#VendorContractDetails").show();
                                    VendorValidationClientSideHide = false;
                                }
                                break;
                            case "VendorGroups":
                                {
                                    CurrentTab = 4;
                                    $("#VendorGroups").addClass("active");
                                    $("#tabGroups").addClass("active in");
                                    VendorValidationClientSideHide = false;
                                }
                                break;
                            default:
                                break;
                        }
                    }
                });
            }
            else if (($('#VendorContractsSaveBtn').hasClass('BackColorRed') || ($('#AddVendorContractBtn').hasClass('BackColorRed')) && CurrentTab == 2)) {
                jConfirm(Messages["ChangeTab"], Messages["ChangeTabTitle"], function (retVal) {
                    if (retVal != true) {
                        event.stopImmediatePropagation();
                        event.preventDefault();
                    }
                    else {
                        $("#VendorContracts").removeClass("active");
                        $("#tabContracts").removeClass("active in");
                        switch ($(CurrentTabElement).attr("id")) {
                            case "VendorGeneral":
                                {
                                    CurrentTab = 1;
                                    $("#VendorGeneral").addClass("active");
                                    $("#tabGeneral").addClass("active in");
                                    VendorValidationClientSideHide = false;
                                }
                                break;
                            case "VendorContractDetails":
                                {
                                    CurrentTab = 3;
                                    $("#VendorContractDetails").addClass("active");
                                    $("#tabContractDetails").addClass("active in");
                                    $("#VendorContractDetails").show();
                                    VendorValidationClientSideHide = false;
                                }
                                break;
                            case "VendorGroups":
                                {
                                    CurrentTab = 4;
                                    $("#VendorGroups").addClass("active");
                                    $("#tabGroups").addClass("active in");
                                    VendorValidationClientSideHide = false;
                                }
                                break;
                            default:
                                break;
                        }
                    }
                });
            }
            else {
                jConfirm(Messages["ChangeTab"], Messages["ChangeTabTitle"], function (retVal) {
                    if (retVal != true) {
                        event.stopImmediatePropagation();
                        event.preventDefault();
                    }
                    else {
                        $("#VendorContractDetails").removeClass("active");
                        $("#tabContractDetails").removeClass("active in");
                        switch ($(CurrentTabElement).attr("id")) {
                            case "VendorGeneral":
                                {
                                    CurrentTab = 1;
                                    $("#VendorGeneral").addClass("active");
                                    $("#tabGeneral").addClass("active in");
                                    VendorValidationClientSideHide = false;
                                }
                                break;
                            case "VendorContracts":
                                {
                                    if (CloseContractDetail) {
                                        $("#VendorContractDetails").removeClass("active");
                                        $("#tabContractDetails").removeClass("active in");
                                        $("#AddVendorContractDetailBtn").removeClass('BackColorRed');
                                        $("#VendorContractDetailsSaveBtn").removeClass('BackColorRed');
                                        $("#VendorContractDetails").hide();
                                        CloseContractDetail = false;
                                    }
                                    CurrentTab = 2;
                                    $("#VendorContracts").addClass("active");
                                    $("#tabContracts").addClass("active in");
                                    VendorValidationClientSideHide = false;
                                }
                                break;
                            case "VendorGroups":
                                {
                                    CurrentTab = 4;
                                    $("#VendorGroups").addClass("active");
                                    $("#tabGroups").addClass("active in");
                                    VendorValidationClientSideHide = false;
                                }
                                break;
                            default:
                                break;
                        }
                    }
                });
            }
        });
    }
});
$(document).on('change', '.VendorDetailsElementChange', function () {
    if (!$('#VendorDetailsSaveBtn').hasClass("disabled")) {
        $('#VendorDetailsSaveBtn').addClass('BackColorRed');
    }
});
$(document).on('change', '.VendorContractsElementChange', function () {
    if (!$('#VendorContractsSaveBtn').hasClass("disabled")) {
        $('#VendorContractsSaveBtn').addClass('BackColorRed');
    }
});
$(document).on('change', '.AddVendorContractElementChange', function () {
    if (!$('#AddVendorContractBtn').hasClass("disabled")) {
        $('#AddVendorContractBtn').addClass('BackColorRed');
    }
});
$(document).on('change', '.VendorContractDetailsElementChange', function () {
    if (!$('#VendorContractDetailsSaveBtn').hasClass("disabled")) {
        $('#VendorContractDetailsSaveBtn').addClass('BackColorRed');
    }
});
$(document).on('change', '.AddVendorContractDetailElementChange', function () {
    if (!$('#AddVendorContractDetailBtn').hasClass("disabled")) {
        $('#AddVendorContractDetailBtn').addClass('BackColorRed');
    }
});
// Money textboxes format 
$(document).on('change', '.MoneyTxb', function () {
    var val = parseFloat(Math.round($(this).val() * 100) / 100).toFixed(2);
    $(this).val(val);
});
function ShowVendor(VendorID) {
    CurrentTab = 1;
    $("#VendorGeneral").addClass("active");
    $("#tabGeneral").addClass("active in");
    VendorIDGlobal = VendorID;
    ShowVendorDetails(VendorID)
    ShowVendorContracts(VendorID);
    ShowVendorGroups(VendorID);
}
function AddNewVendorContractDetailsShow() {
    if ($('#VendorContractDetailsSaveBtn').hasClass('BackColorRed')) {
        jConfirm('Are you sure you want to leave this page? You Have Not Saved Data', 'Warning!', function (retVal) {
            if (retVal == true) {
                $("#VendorContractDetailsDiv").show();
                AddCurrencyDropVal = $("#AddContractDetailSelectedCurrency option:selected").val();
                AddServiceDropVal = $("#AddContractDetailSelectedServiceID option:selected").val();
                $("#VendorSaveCloseAddNewVendorContractDetailBtns").hide();
            }
        });
    }
    else {
        $("#VendorContractDetailsDiv").show();
        AddCurrencyDropVal = $("#AddContractDetailSelectedCurrency option:selected").val();
        AddServiceDropVal = $("#AddContractDetailSelectedServiceID option:selected").val();
        $("#VendorSaveCloseAddNewVendorContractDetailBtns").hide();
    }
}
$(document).on('change', '.AddServiceDrop', function () {
    AddServiceDropVal = this.options[this.selectedIndex].value;
});
$(document).on('change', '.AddCurrencyDrop', function () {
    AddCurrencyDropVal = this.options[this.selectedIndex].value;
});
function AddNewVendorContractDetailsClose() {
    $('#AddVendorContractDetailBtn').removeClass('BackColorRed');
    $("#VendorContractDetailsDiv").hide();
    $("#VendorSaveCloseAddNewVendorContractDetailBtns").show();
}
function AddNewVendorContractShow() {
    if ($('#VendorContractsSaveBtn').hasClass('BackColorRed')) {
        jConfirm('Are you sure you want to leave this page? You Have Not Saved Data', 'Warning!', function (retVal) {
            if (retVal == true) {
                $("#VendorContractsDiv").show();
                AddSeasonCodeDropVal = $("#AddContractSelectedSeasonID option:selected").val();
                $("#VendorSaveCloseAddNewVendorBtns").hide();
            }
        });
    }
    else {
        $("#VendorContractsDiv").show();
        AddSeasonCodeDropVal = $("#AddContractSelectedSeasonID option:selected").val();
        $("#VendorSaveCloseAddNewVendorBtns").hide();
    }
}
function AddNewVendorContractClose() {
    $('#AddVendorContractBtn').removeClass('BackColorRed');
    $("#VendorContractsDiv").hide();
    $("#VendorSaveCloseAddNewVendorBtns").show();
}
function ShowVendorDetails(VendorID) {

    $.ajax({
        url: urls["PreferredVendorLoadPreferredVendorDetails"],
        data: { 'VendorID': VendorID },
        type: 'GET',
        success: function (data) {
            $("#tabGeneral").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error");
        },
        complete: function () {
            ShowVendorDetailPopUp("VendorDetailsDiv");
            InitVendorDetailsDataCalendars();
            InitVendorDetailsDataCalendarsFromTo();
            InitVendorDetailsDataCalendarsFromToDiv();
        }
    });
}
function ShowVendorContracts(VendorID) {
    $.ajax({
        url: urls["PreferredVendorLoadPreferredVendorContracts"],
        data: { 'VendorID': VendorID },
        type: 'GET',
        success: function (data) {
            $("#tabContracts").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error");
        },
        complete: function () {
            InitVendorDetailsDataCalendars();
            InitVendorDetailsDataCalendarsFromTo();
            InitVendorDetailsDataCalendarsFromToDiv();
            BindContractsGridDrop();
        }
    });
}
function VendorContractDetailsClose() {
    CloseContractDetail = true;
    $("#VendorContracts").click();
}
function ShowVendorContractDetails(VendorID, SeasonID) {

    $.ajax({
        url: urls["PreferredVendorLoadPreferredVendorContractDetails"],
        data: { 'VendorID': VendorID, "SeasonID": SeasonID },
        type: 'GET',
        success: function (data) {
            $("#tabContractDetails").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error");
        },
        complete: function () {
            $(".money").maskMoney({ allowNegative: false, thousands: ',', decimal: '.', affixesStay: false });
            $(".percent").maskMoney({ suffix: ' %', allowNegative: false, thousands: ',', decimal: '.', affixesStay: false });
            $("#VendorContractDetails").click();
            InitVendorDetailsDataCalendars();
            InitVendorDetailsDataCalendarsFromTo();
            InitVendorDetailsDataCalendarsFromToDiv();
            BindContractDetailsGridDrop()
        }
    });
}
function ShowVendorGroups(VendorID)
{
    $.ajax({
        url: urls["PreferredVendorLoadPreferredVendorGroups"],
        data: { 'VendorID': VendorID },
        type: 'GET',
        success: function (data) {
            $("#tabGroups").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error");
        },
        complete: function () {
            ShowVendorDetailPopUp("VendorDetailsDiv");
            InitVendorDetailsDataCalendars();
            InitVendorDetailsDataCalendarsFromTo();
            InitVendorDetailsDataCalendarsFromToDiv();
        }
    });
}
$(document).on('click', '#VendorMoreBtn', function () {
    PageNumber = PageNumber + 1;
    LoadVendorGrid()
});
$(document).on('click', '#VendorSearchBtn', function () {
    IsSearchView = true;
    LoadVendorGrid()
});
$(document).on('click', '#AddVendorContractBtn', function () {
    if ($('#VendorContractsSaveBtn').hasClass('BackColorRed')) {
        jConfirm('You Have Not Saved Payments Data. If you add new data you lose unsaved data', 'Warning!', function (retVal) {
            if (retVal == true) {
                AddVendorContract();
            }
        });
    }
    else {
        AddVendorContract();
    }
});
function AddVendorContract() {
    if (isValidAddSeason(AddSeasonCodeDropVal)) {
        if ($('#LoadAddPreferredVendorContractsForm').valid()) {
            var ManageContractsGridViewModel = {
                AddContractSelectedSeasonID: AddSeasonCodeDropVal,
                VendorID: VendorIDGlobal,
                ContractDate: $("#ContractDate").val(),
                ContractStartDate: $("#ContractStartDate").val(),
                ContractEndDate: $("#ContractEndDate").val(),
            };

            var DTO = JSON.stringify(ManageContractsGridViewModel);
            $.ajax({
                url: urls["AddNewPreferredVendorContracts"],
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                data: DTO,
                success: function (data) {
                    $("#VendorContractsDiv").hide();

                },
                error: function (xhr) {
                    jAlert(xhr.statusText, "Error");
                },
                complete: function () {
                    $('#AddVendorContractBtn').removeClass('BackColorRed');
                    jAlert("Data SuccessFully Saved:", "Successful");
                    ShowVendorContracts(VendorIDGlobal);
                }
            });
        }
    }
    else
        jAlert("Vendor contract seasons must be different:", "Error!");
}
$(document).on('click', '#AddVendorContractDetailBtn', function () {
    if ($('#VendorContractDetailsSaveBtn').hasClass('BackColorRed')) {
        jConfirm('You Have Not Saved Payments Data. If you add new data you lose unsaved data', 'Warning!', function (retVal) {
            if (retVal == true) {
                AddVendorContractDetail();
            }
        });
    }
    else {
        AddVendorContractDetail();
    }
});
function AddVendorContractDetail() {
    if ($('#LoadAddPreferredVendorContractDetailsForm').valid()) {
        var ManageVendorContractDetailsGridViewModel = {
            AddContractDetailSelectedServiceID: AddServiceDropVal,
            AddContractDetailSelectedCurrency: AddCurrencyDropVal,
            VendorID: $(".VendorContractDetailsVendorID").val(),
            SeasonID: $(".VendorContractDetailsSelectedSeasonID").val(),
            Description: $("#Description").val(),
            Price: $("#Price").val(),
            StartDate: $("#StartDate").val(),
            EndDate: $("#EndDate").val(),
        };

        var DTO = JSON.stringify(ManageVendorContractDetailsGridViewModel);
        $.ajax({
            url: urls["AddNewPreferredVendorContractDetail"],
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: DTO,
            success: function (data) {
                $("#VendorContractDetailsDiv").hide();

            },
            error: function (xhr) {
                jAlert(xhr.statusText, "Error");
            },
            complete: function () {
                $('#AddVendorContractDetailBtn').removeClass('BackColorRed');
                jAlert("Data SuccessFully Saved:", "Successful");
                ShowVendorContractDetails($(".VendorContractDetailsVendorID").val(), $(".VendorContractDetailsSelectedSeasonID").val())
            }
        });
    }
}
function ShowVendorDetailPopUp(PopUpDiv) {
    $.colorbox({
        width: "1000px",
        height: "710px",
        inline: true,
        href: "#"+PopUpDiv,
        onComplete: function () {
            SelectedCountryID = $("#SelectedCountryID").val();
            Phone800 = $("#Phone800").val();
            PhoneSales = $("#PhoneSales").val();
            FaxSales = $("#FaxSales").val();
            PhoneNumber = $("#PhoneNumber").val();
            FaxNumber = $("#FaxNumber").val();
            FormattedPhoneNumbers(SelectedCountryID);
            $(".phone").mask("(###) ###-####");
            $(".phoneUSACAN").mask("+1 (###) ###-####");
            $(".phoneTR").mask("+90 (###) ###-####");
            $(".phoneFR").mask("+33 (#) ##-##-##-##");
            $(".phoneBEL").mask("+32 (#) ###-##-##");
            $(".phoneSWI").mask("+41 (##) ###-####");
            $(".phoneTS").mask("+216 (##) ###-###");
            $(".phoneALG").mask("+213 (##) ###-####");
            $(".phoneMA").mask("+212 (##) ###-####");
            $("#" + PopUpDiv).show();
        },
        onClosed: function () {
            $("#" + PopUpDiv).hide();
        }
    });
}
$(document).on('click', '#VendorContractDetailsSaveBtn', function () {
    if ($('#VendorContractsSaveBtn').hasClass('BackColorRed') || $('#AddVendorContractBtn').hasClass('BackColorRed')) {
        jConfirm('You Have Not Saved Payments Data. If you add new data you lose unsaved data', 'Warning!', function (retVal) {
            if (retVal == true) {
                var $form = $('#LoadPreferredVendorContractDetailsForm');
                if ($form.valid()) {
                    VendorValidationClientSideHide = false;
                    $.ajax({
                        url: $form.attr('action'),
                        type: $form.attr('method'),
                        data: $form.serialize(),
                        beforeSend: function (xhr) {
                            xhr.overrideMimeType('text/plain; charset=x-user-defined');
                        },
                        success: function (data) {
                            if (data.indexOf("Error") != -1)
                                jAlert(data, "Error");
                        },
                        error: function (xhr) {
                            jAlert(xhr.statusText, "Error");
                        },
                        complete: function () {
                            $('#VendorContractDetailsSaveBtn').removeClass('BackColorRed');
                            jAlert("Data SuccessFully Saved:", "Successful");
                            ShowVendorContractDetails($(".VendorContractDetailsVendorID").val(), $(".VendorContractDetailsSelectedSeasonID").val())
                            ShowVendorContracts($(".VendorContractDetailsVendorID").val());
                        }
                    });
                }
                else
                    VendorValidationClientSideHide = true;
            }
        });
    }
    else {
        var $form = $('#LoadPreferredVendorContractDetailsForm');
        if ($form.valid()) {
            VendorValidationClientSideHide = false;
            $.ajax({
                url: $form.attr('action'),
                type: $form.attr('method'),
                data: $form.serialize(),
                beforeSend: function (xhr) {
                    xhr.overrideMimeType('text/plain; charset=x-user-defined');
                },
                success: function (data) {
                    if (data.indexOf("Error") != -1)
                        jAlert(data, "Error");
                },
                error: function (xhr) {
                    jAlert(xhr.statusText, "Error");
                },
                complete: function () {
                    $('#VendorContractDetailsSaveBtn').removeClass('BackColorRed');
                    jAlert("Data SuccessFully Saved:", "Successful");
                    ShowVendorContractDetails($(".VendorContractDetailsVendorID").val(), $(".VendorContractDetailsSelectedSeasonID").val())
                    ShowVendorContracts($(".VendorContractDetailsVendorID").val());
                }
            });
        }
        else
            VendorValidationClientSideHide = true;
    }

});
function isValidSeasons() {
    var isvalid = true
    var SeasonCodearr = [];
    $(".VendorSeason").each(function (index, elem) {
        var Selectedindex = parseInt(elem.selectedIndex);
        var elemvalue = elem.options[elem.selectedIndex].value;
        SeasonCodearr.push(elemvalue);
    });
    for (var index = 0 ; index < SeasonCodearr.length ; ++index) {
        if (isvalid) {
            for (var index1 = index + 1 ; index1 < SeasonCodearr.length ; ++index1) {
                if (SeasonCodearr[index1] == SeasonCodearr[index]) {
                    isvalid = false;
                    break;
                }
            }
        }
        else
            break;
    }
    return isvalid;
}
function isValidAddSeason(AddSeasonSelectedindex) {
    var isvalid = true
    var SeasonCodearr = [];
    $(".VendorSeason").each(function (index, elem) {
        var Selectedindex = parseInt(elem.selectedIndex);
        var elemvalue = elem.options[elem.selectedIndex].value;
        SeasonCodearr.push(elemvalue);
    });
    for (var index = 0 ; index < SeasonCodearr.length ; ++index) {
        if (AddSeasonSelectedindex == SeasonCodearr[index]) {
            isvalid = false;
            break;
        }
    }
    return isvalid;
}
$(document).on('click', '#VendorContractsSaveBtn', function () {
    if (isValidSeasons()) {
        var $form = $('#LoadPreferredVendorContractsForm');
        if ($form.valid()) {
            VendorValidationClientSideHide = false;
            $.ajax({
                url: $form.attr('action'),
                type: $form.attr('method'),
                data: $form.serialize(),
                beforeSend: function (xhr) {
                    xhr.overrideMimeType('text/plain; charset=x-user-defined');
                },
                success: function (data) {
                    if (data.indexOf("Error") != -1)
                        jAlert(data, "Error");
                },
                error: function (xhr) {
                    jAlert(xhr.statusText, "Error");
                },
                complete: function () {
                    $('#VendorContractsSaveBtn').removeClass('BackColorRed');
                    jAlert("Data SuccessFully Saved:", "Successful");
                    ShowVendorContracts(VendorIDGlobal);
                }
            });
        }
        else
            VendorValidationClientSideHide = true;
    }
    else
        jAlert("Vendor contract seasons must be different:", "Error!");
});
$(document).on('click', '#VendorDetailsSaveBtn', function () {
    var $form = $('#LoadPreferredVendorDetailsForm');
    if ($form.valid()) {
        VendorValidationClientSideHide = false;
        $.ajax({
            url: $form.attr('action'),
            type: $form.attr('method'),
            data: $form.serialize(),
            beforeSend: function (xhr) {
                xhr.overrideMimeType('text/plain; charset=x-user-defined');
            },
            success: function (data) {
                if (data.indexOf("Error") != -1)
                    jAlert(data, "Error");
            },
            error: function (xhr) {
                jAlert(xhr.statusText, "Error");
            },
            complete: function () {
                $('#VendorDetailsSaveBtn').removeClass('BackColorRed')
                jAlert("Data SuccessFully Saved:", "Successful");
                ShowVendorDetails(VendorIDGlobal)
            }
        });
    }
    else
        VendorValidationClientSideHide = true;
});
function InitVendorDetailsDataCalendars() {

    $(".VendorDeteilsDataCalendar").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 1,
        dateFormat: 'dd/M/yy',
        beforeShow: function (input) {
            $(input).css({
                "position": "relative",
                "z-index": 999999
            });
        }
    });
}
function InitVendorDetailsDataCalendarsFromTo() {
    $(".VendorDeteilsDataCalendarFrom").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 1,
        dateFormat: 'dd/M/yy',
        beforeShow: function (input) {
            $(input).css({
                "position": "relative",
                "z-index": 999999,
            });
        },
        onClose: function (selectedDate) {
            $(this).parent().parent().find(".VendorDeteilsDataCalendarTo").datepicker("option", "minDate", selectedDate);
        }
    });
    $(".VendorDeteilsDataCalendarTo").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 1,
        dateFormat: 'dd/M/yy',
        beforeShow: function (input) {
            $(input).css({
                "position": "relative",
                "z-index": 999999,
            });
        },
        onClose: function (selectedDate) {
            $(this).parent().parent().find(".VendorDeteilsDataCalendarFrom").datepicker("option", "maxDate", selectedDate);
        }
    });
}

function InitVendorDetailsDataCalendarsFromToDiv() {
    $(".VendorDeteilsDataCalendarFromDiv").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 1,
        dateFormat: 'dd/M/yy',
        beforeShow: function (input) {
            $(input).css({
                "position": "relative",
                "z-index": 999999,
            });
        },
        onClose: function (selectedDate) {
            $(".VendorDeteilsDataCalendarToDiv").datepicker("option", "minDate", selectedDate);
        }
    });
    $(".VendorDeteilsDataCalendarToDiv").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 1,
        dateFormat: 'dd/M/yy',
        beforeShow: function (input) {
            $(input).css({
                "position": "relative",
                "z-index": 999999,
            });
        },
        onClose: function (selectedDate) {
            $(".VendorDeteilsDataCalendarFromDiv").datepicker("option", "maxDate", selectedDate);
        }
    });
}

function LoadVendorGrid()
{
    var ManagePreferredVendorsGridViewModel = {
        RegionSearchText: $("#RegionSearchText").val(),
        AreaSearchText: $("#AreaSearchText").val(),
        CitySearchText: $("#CitySearchText").val(),
        StateSearchText: $("#StateSearchText").val(),
        SelectedSearchVendorTypeID: $("#SelectedSearchVendorTypeID").val(),
        PageNum: parseInt(PageNumber),
        IsSearch: IsSearchView,
    };
    
    var DTO = JSON.stringify(ManagePreferredVendorsGridViewModel);
    $.ajax({
        url: urls["PreferredVendorLoadPreferredVendorsGrid"],
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: DTO,
        success: function (data) {
            $("#ManageVendorGrid").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error");
        },
        complete: function () {

        }
    });
}
$(document).on('change', '.AddSeasonCodeDrop', function () {
    AddSeasonCodeDropVal = this.options[this.selectedIndex].value;
    var SeasonDescription = $("#LoadPreferredVendorContractsForm #SelectedSeasonDescription")[0].options[this.selectedIndex].text;
    $("#LoadAddPreferredVendorContractsForm .AddSeasonDesc").text(SeasonDescription);
});
// status dropdown bug over type
$(document).on('change', '.SeasonCodeDrop', function () {
    $(this).next().prop("selectedIndex", $(this).prop("selectedIndex"));
    var SeasonDescription = $("#LoadPreferredVendorContractsForm #SelectedSeasonDescription")[0].options[this.selectedIndex].text;
    var element = $(this).parent();
    var tdparent = element.parent();
    $(tdparent).find(".SeasonDesc").text(SeasonDescription);
});
function BindContractsGridDrop() {
    $(".SeasonCodeDrop").each(function (index, elem) {
        $(elem).next().prop("selectedIndex", $(elem).prop("selectedIndex"));
    });
}
function BindContractDetailsGridDrop() {
    $(".ServiceDrop").each(function (index, elem) {
        $(elem).next().prop("selectedIndex", $(elem).prop("selectedIndex"));
    });
}
$(document).on('change', '.ServiceDrop', function () {
    $(this).next().prop("selectedIndex", $(this).prop("selectedIndex"));
});
$(document).on('click', '#StartUpload', function () {
    var data = new FormData();
    var files = $('#UploadVendorContractFilePath').get(0).files;

    if ($(files).length > 0) {
        data.append('UploadedFiles', files[0]);
        $.ajax({
            type: 'POST',
            url: '/PreferredVendor/UploadPreferredVendorContractFile?VendorID=' + $("#ContractTableDetail_VendorID").val() + '&SeasonID=' + $("#ContractTableDetail_SelectedSeasonID").val(),
            data: data,
            dataType: 'json', // have tried this ajax without dataType, same problem
            contentType: false,
            processData: false,
            success: function (data) {
                jAlert(data.message, "Error");
            },
            error: function (param1, param2, param3) {
                jAlert("Error!", "Error");
            }
        });
    }
    else
        jAlert("Please Choose File For Upload", "Error");
});

$(document).on('click', '#Download', function () {
    if ($("#ContractFIleHidden").val() != null && $("#ContractFIleHidden").val().length > 0) {
        $.colorbox({
            iframe: true,
            innerWidth: 1100,
            innerHeight: 700,
            // href: "/VendorContracts/4/14/P1020276.JPG",
            href: $("#ContractFIleHidden").val(),
        });
    }
    else
        jAlert("No contract to show!", "Warning!");
    //window.open('../PreferredVendor/DownloadPreferredVendorContractFile?FileName=' + $("#ContractFIleHidden").val())
});
$(document).on('change', '#UploadVendorContractFilePath', function () {
    $("#DownloadDiv").hide();
});
$(document).on('change', '.CountryDrop', function () {
    FormattedPhoneNumbers($(this).val());
    if ($(this).val() === "") {
        $("#StateDiv").hide();
        $("#CityDiv").hide();
        $("#StateDivChange").show();
        $("#CityDivChange").show();
        $.ajax({
            url: urls["PreferredVendorGetRegionByCountryID"],
            data: { CountryID: 0 },
            cache: false,
            type: "POST",
            success: function (data) {
                var markup = "<option value='0'>Select</option>";
                for (var x = 0; x < data.length; x++) {
                    markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
                }
                $("Select.RegionDrop").html(markup);
            },
            error: function (reponse) {
                jAlert(reponse, "Error");
            },
            complete: function () {
            }
        });
    }
    else {
        $.ajax({
            url: urls["PreferredVendorGetRegionByCountryID"],
            data: { CountryID: parseInt($(this).val()) },
            cache: false,
            type: "POST",
            success: function (data) {
                var markup = "<option value='0'>Select</option>";
                for (var x = 0; x < data.length; x++) {
                    markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
                }
                $("Select.RegionDrop").html(markup);
            },
            error: function (reponse) {
                jAlert(reponse, "Error");
            },
            complete: function () {
            }
        });
    }
    if ($(this).val() === '1') {
        $("#CityDiv").hide();
        $("#CityDivChange").show();
    }
    if ($(this).val() === '1') {
        $.ajax({
            url: urls["PreferredVendorGetStateByCountryID"],
            data: { CountryID: parseInt($(this).val()) },
            cache: false,
            type: "POST",
            success: function (data) {
                var markup = "<option value='0'>Select</option>";
                for (var x = 0; x < data.length; x++) {
                    markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
                }
                $("Select.StateDrop").html(markup);
            },
            error: function (reponse) {
                jAlert(reponse, "Error");
            },
            complete: function () {
                $("#StateDiv").show();
                $("#StateDivChange").hide();
            }
        });
    }
    else if ($(this).val() != "") {
        $.ajax({
            url: urls["PreferredVendorGetCityByCountryID"],
            data: { CountryID: parseInt($(this).val()), StateID: '0' },
            cache: false,
            type: "POST",
            success: function (data) {
                var markup = "<option value='0'>Select</option>";
                for (var x = 0; x < data.length; x++) {
                    markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
                }
                $("Select.CityDrop").html(markup);
            },
            error: function (reponse) {
                jAlert(reponse, "Error");
            },
            complete: function () {
                $("#StateDiv").hide();
                $("#StateDivChange").show();
                $("#CityDiv").show();
                $("#CityDivChange").hide();
            }
        });
    }
    $.ajax({
        url: urls["PreferredVendorGetAreaByRegionID"],
        data: { RegionID: 0 },
        cache: false,
        type: "POST",
        success: function (data) {
            var markup = "<option value='0'>Select</option>";
            for (var x = 0; x < data.length; x++) {
                markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
            }
            $("Select.AreaDrop").html(markup);
        },
        error: function (reponse) {
            jAlert(reponse, "Error");
        },
        complete: function () {
        }
    });
});
$(document).on('change', '.RegionDrop', function () {
    $.ajax({
        url: urls["PreferredVendorGetAreaByRegionID"],
        data: { RegionID: parseInt($(this).val()) },
        cache: false,
        type: "POST",
        success: function (data) {
            var markup = "<option value='0'>Select</option>";
            for (var x = 0; x < data.length; x++) {
                markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
            }
            $("Select.AreaDrop").html(markup);
        },
        error: function (reponse) {
            jAlert(reponse, "Error");
        },
        complete: function () {
        }
    });
});
$(document).on('change', '.StateDrop', function () {
    var StateID = $(this).val();
    $.ajax({
        url: urls["PreferredVendorGetCityByCountryID"],
        data: { CountryID: 0, StateID: StateID },
        cache: false,
        type: "POST",
        success: function (data) {
            var markup = "<option value='0'>Select</option>";
            for (var x = 0; x < data.length; x++) {
                markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
            }
            $("Select.CityDrop").html(markup);
        },
        error: function (reponse) {
            jAlert(reponse, "Error");
        },
        complete: function () {
            if (StateID === "" || StateID === "0") {
                $("#CityDiv").hide();
                $("#CityDivChange").show();
            }
            else {
                $("#CityDiv").show();
                $("#CityDivChange").hide();
            }
        }
    });
});
$(document).on('change', '#Phone800', function () {
    Phone800 = $(this).val();
});
$(document).on('change', '#PhoneNumber', function () {
    PhoneNumber = $(this).val();
});
$(document).on('change', '#FaxNumber', function () {
    FaxNumber = $(this).val();
});
$(document).on('change', '#PhoneSales', function () {
    PhoneSales = $(this).val();
});
$(document).on('change', '#FaxSales', function () {
    FaxSales = $(this).val();
});
$(document).on('change', '#PhoneNumberUnformatted', function () {
    if ($("#PhoneNumberUnformatted").prop('checked') == true) {
        $(".phone").unmask();
        $(".phoneUSACAN").unmask();
        $(".phoneTR").unmask();
        $(".phoneFR").unmask();
        $(".phoneBEL").unmask();
        $(".phoneSWI").unmask();
        $(".phoneTS").unmask();
        $(".phoneALG").unmask();
        $(".phoneMA").unmask();

        $("#Phone800").addClass("number");
        $("#PhoneNumber").addClass("number");
        $("#FaxNumber").addClass("number");
        $("#PhoneSales").addClass("number");
        $("#FaxSales").addClass("number");
        $(".number").maskMoney({ prefix: '', allowNegative: false, thousands: '', decimal: '', affixesStay: false, precision: 0 });

        $("#Phone800").val(Phone800);
        $("#PhoneNumber").val(PhoneNumber);
        $("#FaxNumber").val(FaxNumber);
        $("#PhoneSales").val(PhoneSales);
        $("#FaxSales").val(FaxSales);
    }
    else {
        $(".number").unmask();
        $("#Phone800").removeClass("number");
        $("#PhoneNumber").removeClass("number");
        $("#FaxNumber").removeClass("number");
        $("#PhoneSales").removeClass("number");
        $("#FaxSales").removeClass("number");
        FormattedPhoneNumbers($("#SelectedCountryID").val())
    }
});
function FormattedPhoneNumbers(CountryID) {
    if ($("#PhoneNumberUnformatted").prop('checked') != true) {
        $("#Phone800").val(Phone800);
        $("#PhoneNumber").val(PhoneNumber);
        $("#FaxNumber").val(FaxNumber);
        $("#PhoneSales").val(PhoneSales);
        $("#FaxSales").val(FaxSales);
        switch (CountryID) {
            case '1': case '7':
                {
                    if (SelectedCountryID == '1' || SelectedCountryID == '7') {
                        $("#Phone800").val(Phone800);
                        $("#PhoneNumber").val(PhoneNumber);
                        $("#FaxNumber").val(FaxNumber);
                        $("#PhoneSales").val(PhoneSales);
                        $("#FaxSales").val(FaxSales);
                    }
                    $("#Phone800").removeAttr('class');
                    $("#Phone800").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneUSACAN');
                    $("#PhoneSales").removeAttr('class');
                    $("#PhoneSales").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneUSACAN');
                    $("#FaxSales").removeAttr('class');
                    $("#FaxSales").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneUSACAN');
                    $("#PhoneNumber").removeAttr('class');
                    $("#PhoneNumber").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneUSACAN');
                    $("#FaxNumber").removeAttr('class');
                    $("#FaxNumber").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneUSACAN');
                    $(".phoneUSACAN").mask("+1 (###) ###-####");
                };
                break;
            case '3':
                {
                    $("#Phone800").removeAttr('class');
                    $("#Phone800").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneTR');
                    $("#PhoneSales").removeAttr('class');
                    $("#PhoneSales").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneTR');
                    $("#FaxSales").removeAttr('class');
                    $("#FaxSales").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneTR');
                    $("#PhoneNumber").removeAttr('class');
                    $("#PhoneNumber").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneTR');
                    $("#FaxNumber").removeAttr('class');
                    $("#FaxNumber").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneTR');
                    $(".phoneTR").mask("+90 (###) ###-####");
                };
                break;
            case '5':
                {
                    $("#Phone800").removeAttr('class');
                    $("#Phone800").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneFR');
                    $("#PhoneSales").removeAttr('class');
                    $("#PhoneSales").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneFR');
                    $("#FaxSales").removeAttr('class');
                    $("#FaxSales").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneFR');
                    $("#PhoneNumber").removeAttr('class');
                    $("#PhoneNumber").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneFR');
                    $("#FaxNumber").removeAttr('class');
                    $("#FaxNumber").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneFR');
                    $(".phoneFR").mask("+33 (#) ##-##-##-##");
                };
                break
            case '8':
                {
                    $("#Phone800").removeAttr('class');
                    $("#Phone800").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneBEL');
                    $("#PhoneSales").removeAttr('class');
                    $("#PhoneSales").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneBEL');
                    $("#FaxSales").removeAttr('class');
                    $("#FaxSales").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneBEL');
                    $("#PhoneNumber").removeAttr('class');
                    $("#PhoneNumber").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneBEL');
                    $("#FaxNumber").removeAttr('class');
                    $("#FaxNumber").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneBEL');
                    $(".phoneBEL").mask("+32 (#) ###-##-##");
                };
                break
            case '11':
                {
                    $("#Phone800").removeAttr('class');
                    $("#Phone800").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneSWI');
                    $("#PhoneSales").removeAttr('class');
                    $("#PhoneSales").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneSWI');
                    $("#FaxSales").removeAttr('class');
                    $("#FaxSales").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneSWI');
                    $("#PhoneNumber").removeAttr('class');
                    $("#PhoneNumber").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneSWI');
                    $("#FaxNumber").removeAttr('class');
                    $("#FaxNumber").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneSWI');
                    $(".phoneSWI").mask("+41 (##) ###-####");
                };
                break
            case '12':
                {
                    $("#Phone800").removeAttr('class');
                    $("#Phone800").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneTS');
                    $("#PhoneSales").removeAttr('class');
                    $("#PhoneSales").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneTS');
                    $("#FaxSales").removeAttr('class');
                    $("#FaxSales").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneTS');
                    $("#PhoneNumber").removeAttr('class');
                    $("#PhoneNumber").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneTS');
                    $("#FaxNumber").removeAttr('class');
                    $("#FaxNumber").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneTS');
                    $(".phoneTS").mask("+216 (##) ###-###");
                };
                break
            case '24':
                {
                    $("#Phone800").removeAttr('class');
                    $("#Phone800").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneALG');
                    $("#PhoneSales").removeAttr('class');
                    $("#PhoneSales").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneALG');
                    $("#FaxSales").removeAttr('class');
                    $("#FaxSales").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneALG');
                    $("#PhoneNumber").removeAttr('class');
                    $("#PhoneNumber").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneALG');
                    $("#FaxNumber").removeAttr('class');
                    $("#FaxNumber").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneALG');
                    $(".phoneALG").mask("+213 (##) ###-####");
                };
                break
            case '26':
                {
                    $("#Phone800").removeAttr('class');
                    $("#Phone800").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneMA');
                    $("#PhoneSales").removeAttr('class');
                    $("#PhoneSales").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneMA');
                    $("#FaxSales").removeAttr('class');
                    $("#FaxSales").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneMA');
                    $("#PhoneNumber").removeAttr('class');
                    $("#PhoneNumber").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneMA');
                    $("#FaxNumber").removeAttr('class');
                    $("#FaxNumber").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phoneMA');
                    $(".phoneMA").mask("+212 (##) ###-####");
                };
                break
            default:
                {
                    $("#Phone800").removeAttr('class');
                    $("#Phone800").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phone');
                    $("#PhoneSales").removeAttr('class');
                    $("#PhoneSales").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phone');
                    $("#FaxSales").removeAttr('class');
                    $("#FaxSales").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phone');
                    $("#PhoneNumber").removeAttr('class');
                    $("#PhoneNumber").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phone');
                    $("#FaxNumber").removeAttr('class');
                    $("#FaxNumber").addClass('VendorDetailsElementChange table_inputs_SmallPadding2 inpputgr form-control phone');
                    $(".phone").mask("(###) ###-####");
                };
        }
    }
}

var originalClose = $.colorbox.close;
$.colorbox.close = function () {
    if ($('#VendorDetailsSaveBtn').hasClass('BackColorRed') || $('#VendorContractsSaveBtn').hasClass('BackColorRed') || $('#AddVendorContractBtn').hasClass('BackColorRed') || $('#VendorContractDetailsSaveBtn').hasClass('BackColorRed') || $('#AddVendorContractDetailBtn').hasClass('BackColorRed')) {
        jConfirm(Messages["ClosePopUp"], Messages["ClosePopUpTitle"], function (retVal) {
            if (retVal == true) {
                $("#tabGeneral").empty();
                $("#tabGroups").empty();
                $("#tabContracts").empty();
                $("#tabContractDetails").empty();
                $("#tabGeneral").removeClass("active in");
                $("#tabGroups").removeClass("active in");
                $("#tabContracts").removeClass("active in");
                $("#tabContractDetails").removeClass("active in");
                $("#VendorContractDetails").hide();
                $("#VendorContractDetails").removeClass("active");
                $("#VendorContracts").removeClass("active");
                $("#VendorGroups").removeClass("active");
                $("#VendorGeneral").removeClass("active");
                LoadVendorGrid();
                originalClose();
            }
        });
    }
    else {
        $("#tabGeneral").empty();
        $("#tabGroups").empty();
        $("#tabContracts").empty();
        $("#tabContractDetails").empty();
        $("#tabGeneral").removeClass("active in");
        $("#tabGroups").removeClass("active in");
        $("#tabContracts").removeClass("active in");
        $("#tabContractDetails").removeClass("active in");
        $("#VendorContractDetails").hide();
        $("#VendorContractDetails").removeClass("active");
        $("#VendorContracts").removeClass("active");
        $("#VendorGroups").removeClass("active");
        $("#VendorGeneral").removeClass("active");
        LoadVendorGrid();
        originalClose();
    }
};

$(document).on('change', '.ElementsForValidation', function () {
    if ($(this).valid()) {
        $(this).closest("form").find("#errorMessageClient").text("");
    }
    else
        $(this).closest("form").find("#errorMessageServer").hide();
    if (VendorValidationClientSideHide) {
        if ($(this).closest("form").valid()) {
            $(this).closest("form").find("#errorMessageClient").text("");
        }
    }
    else if ($(this).valid()) {
        var elements = "#" + $(this).closest("form").attr("id") + " .error";
        $(elements).not('label').each(function () {
            $(this).valid();
        });
    }
});


